package online.longzipeng.householdbillssystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.longzipeng.householdbillssystem.business.entity.Bill;
import online.longzipeng.householdbillssystem.business.entity.BillGroup;
import online.longzipeng.householdbillssystem.business.vo.bill.QueryBillVO;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.mapper.BillMapper;
import online.longzipeng.householdbillssystem.mapper.GroupMapper;
import online.longzipeng.householdbillssystem.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {

    @Autowired
    GroupMapper groupMapper;

    @Override
    public Bill findById(Integer id) {
        return getById(id);
    }

    @Override
    public List<Bill> findFromGroupByUserGroupId(Integer id) {
        // 查询用户组下所有的账单
//        QueryWrapper<BillGroup> qw = new QueryWrapper<>();
//        qw.eq("group_id", id);
//        List<BillGroup> billGroups = billGroupMapper.selectList(qw);
//        List<Integer> billIds = billGroups.stream().map(BillGroup::getBillId).collect(Collectors.toList());
        List<Bill> bills = new ArrayList<>();
//        for (Integer billId : billIds) {
//            Bill bill = getById(billId);
//            if (bill != null) {
//                bills.add(bill);
//            }
//        }
        return bills;
    }


    @Override
    public List<Bill> findFromUserByUserId(Integer userId) {
        // 先查询用户创建的订单
//        QueryWrapper<Bill> qwBill = new QueryWrapper<>();
//        qwBill.eq("user_id",userId);
//        List<Bill> billsByUser = list(qwBill);
//        if (!CollectionUtils.isEmpty(billsByUser)) {
//            // 再查询添加到用户组中的账单，进行去重
//            List<Integer> ids = billsByUser.stream().map(Bill::getId).collect(Collectors.toList());
//            List<BillGroup> billGroups = billGroupMapper.selectByBillIds(ids);
//            if (CollectionUtils.isEmpty(billGroups)){
//                return billsByUser;
//            }
//            ids.removeAll(billGroups.stream().map(BillGroup::getBillId).collect(Collectors.toList()));
//            List<Bill> collect = billsByUser.stream().filter(bill -> ids.contains(bill.getId())).collect(Collectors.toList());
//            return collect;
//        }
        return null;
    }

    @Override
    public boolean delete(Integer id) {
        // 先删除用户组中的相关账单再删除bill中的账单
//        QueryWrapper<BillGroup> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("bill_id",id);
//        billGroupMapper.delete(queryWrapper);
        return removeById(id);
    }

    @Override
    public boolean add(Bill bill) {
        ExceptionUtils.isNotNull(getById(bill.getUserId()),"创建者用户不存在！！请检查userId");
        return save(bill);
    }

    @Override
    public IPage<Bill> getListPrivatePage(QueryBillVO queryVO,Long userId) {
        Page<Bill> billPage = new Page(queryVO.getPage(),queryVO.getSize());
        QueryWrapper<Bill> queryBillWrapper = new QueryWrapper<>();
        queryBillWrapper.eq("user_id",userId);
        billPage = page(billPage,queryBillWrapper);
        return billPage;
    }

    @Override
    public IPage<Bill> getAllListPage(QueryBillVO queryVO,Long userId) {
        Page<Bill> billPage = new Page(queryVO.getPage(),queryVO.getSize());
        billPage = page(billPage);
        return billPage;
    }

    @Override
    public IPage<Bill> getListGroupPage(QueryBillVO queryVO,Long userId) {
        return null;
    }

    @Override
    public IPage<Bill> getListPage(QueryBillVO queryVO, Long userId) {
        return null;
    }

}